path_data=normalizePath('../../2021-2022(2)')
class_data_files = list.files(path_data)

library(readxl)
library(haven)
library(expss)
class_id = 2
xls_file = normalizePath(file.path(path_data,class_data_files[class_id]))
df_class_data = read_excel(xls_file,col_names = c('id','student_id','name'))

generate_data = function(seed_num,student_name){
  set.seed(seed_num)
  output_folder = file.path(getwd(),'data','chapter05',paste(seed_num,student_name,sep="_"))
  dir.create(file.path(getwd(),'data','chapter05'))
  dir.create(output_folder)
  iq=1:6
  method1 = round(rnorm(6,86,4.8),1)
  method2 = round(rnorm(6,80.5,5),1)
  method3 = round(rnorm(6,94,4.7),1)
  df = data.frame(
    iq = iq,
    method1 = method1,
    method2 = method2,
    method3 = method3
  )
  df$iq = factor(df$iq,labels=c('C-','C','B-','B','A-','A'),ordered = TRUE)
  df=apply_labels(df,
                  iq="IQ区块",method1="方法1",method2="方法2",method3="方法3")
  write_sav(df,file.path(output_folder,'ch5_exe_1.sav'))
  return(df)
}

for (row in 1:nrow(df_class_data)){
  student_id =as.numeric(df_class_data[row,'student_id']) %% 1000000
  name=df_class_data[row,'name']
  generate_data(student_id,name)
}
